// pages/cate/cate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    count: 5,
    flag: false,
    height:500,
    moveList: [], //定义要渲染的电影列表变量
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMovieList()
   let info= wx.getSystemInfoSync()
   console.log('info:',info)

   this.setData({
     height:info.windowHeight
   })
  },
  getMovieList() {
    let url = 'https://api.douban.com/v2/movie/top250?apikey=0b2bdeda43b5688921839c8ecb20399b'

    this.setData({
      flag: false
    })

    wx.showLoading({
      title: '正在加载中。。。',
    })

    /*
      page(页码)   start（从第几条开始）   count：每页几条
        1            0                    5
        2            5                    5
        3            10                   5
        4            15                   5
 
     start=(page-1)*count
    
     */

    let { page, count } = this.data
    wx.request({
      url: url,
      data: {
        start: (page - 1) * count,
        count
      },
      header: {
        'content-type': 'application/text'
      },
      success: (res) => {
        //  console.log(res)
        let { statusCode, data } = res;
        let { subjects } = data;
        console.log(statusCode)
        console.log(data)
        console.log(subjects)

        if (statusCode === 200) {
          page++

          this.data.moveList = this.data.moveList.concat(subjects)
          this.setData({
            moveList: this.data.moveList,
            page,
            flag: true
          })

          wx.hideLoading()
        }
      }
    })

  },
  lower: function () {
    console.log(window)
    console.log(document)
    console.log('触底加载')
    if (this.data.flag) {
      this.getMovieList()
    }
  }
})